package com.projetosi.web.client;

import com.extjs.gxt.charts.client.model.axis.Label;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.WindowEvent;
import com.extjs.gxt.ui.client.event.WindowListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.MultiField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */

public class ProjetoSI_WEB implements EntryPoint {
	
	private final PersistenceServiceAsync persistenceService = GWT
			.create(PersistenceService.class);
	
	public void teste(){
		persistenceService.teste(new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				// Se o call no servidor der certo, esse result ai que ele vai receber é o que tu retornou no servidor
				Info.display("..", result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// Se cair aqui a chamada deu pau.
				
			}
		});
	}
	
	public void onModuleLoad() {
		
		final Window w = new Window();
		final FormPanel formLogin = new FormPanel();
		final FormPanel panel = criarPanel();
		
		w.setPlain(true);
		w.setSize(300, 240);
		w.setHeading("Cadastro");
		w.setLayout(new FitLayout());
		w.setResizable(false);
		
		LabelField userLabel = new LabelField();
		userLabel.setText("Usuario: ");
		LabelField passLabel = new LabelField();
		passLabel.setText("Senha: ");
		
		ListStore<Label> siglasEstados = new ListStore<Label>();
		siglasEstados.add(new Label("PB"));
		TextField<String> userNameField = new TextField<String>();
		TextField<String> passWordField = new TextField<String>();
		final TextField<String> nomeField = new TextField<String>();
		final TextField<String> loginField = new TextField<String>();
		final TextField<String> senhaField = new TextField<String>();
		TextField<String> confirmarSenhaField = new TextField<String>();
		TextField<String> cidadeField = new TextField<String>();
		ComboBox<Label> estadoBox = new ComboBox<Label>();
		estadoBox.setEditable(false);
		estadoBox.setStore(siglasEstados);
		estadoBox.setWidth(60);
		final TextField<String> enderecoField = new TextField<String>();
		
		
	    nomeField.setFieldLabel("Nome ");
	    loginField.setFieldLabel("Login ");
	    senhaField.setFieldLabel("Senha ");
	    confirmarSenhaField.setFieldLabel("Confirmar senha ");
	    senhaField.setPassword(true);
	    confirmarSenhaField.setPassword(true);
	    enderecoField.setFieldLabel("Endereco");
	    
		userNameField.setName("user");
		passWordField.setName("pass");
		passWordField.setPassword(true);
		userNameField.setAllowBlank(false);
		
		MultiField<Widget> multiFieldUser = new MultiField<Widget>();
		MultiField<Widget> multiFieldPass = new MultiField<Widget>();
		MultiField<Widget> multiFieldButton = new MultiField<Widget>();
		MultiField<Widget> multiEndereco = new MultiField<Widget>();

		
		multiFieldButton.setLabelSeparator("");
		multiFieldPass.setLabelSeparator("");
		multiFieldUser.setLabelSeparator("");
		
		multiFieldPass.setSpacing(16);
		multiFieldPass.add(passLabel);
		multiFieldPass.add(passWordField);
		multiFieldUser.setSpacing(9);
		multiFieldUser.add(userLabel);
		multiFieldUser.add(userNameField);
		multiFieldButton.setSpacing(109);
		multiEndereco.add(cidadeField);
		multiEndereco.add(new AdapterField(estadoBox));
		multiEndereco.setFieldLabel("Cidade ");
		
		Button logar = new Button("Logar", new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				TelaInicial2 tela = new TelaInicial2();
				tela.onModuleLoad();
				
			}
		});
		
		Button cadastrarBt = new Button("Cadastrar", new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				w.show();
			}
		});
		
		panel.addButton(new Button("Cancelar", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				persistenceService.teste(new AsyncCallback<String>() {
					
					@Override
					public void onSuccess(String result) {
						com.google.gwt.user.client.Window.alert(result);
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						com.google.gwt.user.client.Window.alert("Fail");
						
					}
				});
				w.hide();
			}
		}));
		
		panel.addButton(new Button("Confirmar", new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				//TODO logica a ser implementada
				String login = loginField.getValue();
				String nome = nomeField.getValue();
				String senha = senhaField.getValue();
				String endereco = enderecoField.getValue();
				persistenceService.addUsuario(login, nome, senha, endereco, new AsyncCallback<Void>() {

					@Override
					public void onFailure(Throwable caught) {
						com.google.gwt.user.client.Window.alert(caught.getMessage());
					}

					@Override
					public void onSuccess(Void result) {
						com.google.gwt.user.client.Window.alert("Usuário cadastrado com sucesso!");
						
					}
					
				});
//				MessageBox message = new MessageBox();
//				message.setMessage("Cadastro OK!");
//				message.show();
				
			}
		}));		
		
		multiFieldButton.add(new AdapterField(logar));
		multiFieldButton.add(new AdapterField(cadastrarBt));
		
		formLogin.setWidth(400);
		formLogin.add(multiFieldUser);
		formLogin.add(multiFieldPass);
		formLogin.add(multiFieldButton);
		formLogin.setHeading("MEmprestai");
		
		w.addWindowListener(new WindowListener(){
			@Override
			public void windowShow(WindowEvent we){
				formLogin.setEnabled(false);
			}
			
			@Override
			public void windowHide(WindowEvent we){
				formLogin.setEnabled(true);
			}
		});
		
		povoarPanel(panel, nomeField);
		povoarPanel(panel, multiEndereco);
		povoarPanel(panel, enderecoField);
		povoarPanel(panel, loginField);
		povoarPanel(panel, senhaField);
		povoarPanel(panel, confirmarSenhaField);
		w.add(panel);
		RootPanel.get("fieldContainer").add(formLogin);
	}
	
	private void povoarPanel(final FormPanel panel, Widget widget) {
		panel.add(widget);
	}
	
	private FormPanel criarPanel(){
		FormPanel panel = new FormPanel();
	    panel.setBorders(false);
	    panel.setBodyBorder(false);
	    panel.setLabelWidth(55);
	    panel.setPadding(5);
	    panel.setHeaderVisible(false);
	    return panel;
	}
}
